{% extends "admin/base.html" %}
{% set active_page = 'scales' %}

{% block title %}量表详情{% endblock %}

{% block page_title %}量表详情{% endblock %}

{% block page_subtitle %}查看量表基本信息、统计数据和测评记录{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- 量表基本信息 -->
    <div class="row mb-4">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-clipboard-data me-2"></i>基本信息</h5>
                </div>
                <div class="card-body">
                    <table class="table table-borderless">
                        <tr>
                            <td width="20%"><strong>量表ID:</strong></td>
                            <td>{{ scale.id }}</td>
                        </tr>
                        <tr>
                            <td><strong>量表名称:</strong></td>
                            <td>{{ scale.title }}</td>
                        </tr>
                        <tr>
                            <td><strong>量表描述:</strong></td>
                            <td>{{ scale.description or '-' }}</td>
                        </tr>
                        <tr>
                            <td><strong>分类:</strong></td>
                            <td>
                                {% if scale.category %}
                                    <span class="badge bg-primary">{{ scale.category }}</span>
                                {% else %}
                                    -
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <td><strong>题目数量:</strong></td>
                            <td>{{ scale.questions|length if scale.questions else 0 }} 题</td>
                        </tr>
                        <tr>
                            <td><strong>创建时间:</strong></td>
                            <td>{{ scale.created_at.strftime('%Y-%m-%d %H:%M:%S') if scale.created_at else '-' }}</td>
                        </tr>
                        <tr>
                            <td><strong>更新时间:</strong></td>
                            <td>{{ scale.updated_at.strftime('%Y-%m-%d %H:%M:%S') if scale.updated_at else '-' }}</td>
                        </tr>
                        <tr>
                            <td><strong>状态:</strong></td>
                            <td>
                                {% if scale.status == 'PUBLISHED' %}
                                    <span class="badge bg-success">已发布</span>
                                {% elif scale.status == 'DRAFT' %}
                                    <span class="badge bg-warning">草稿</span>
                                {% elif scale.status == 'ARCHIVED' %}
                                    <span class="badge bg-secondary">已归档</span>
                                {% endif %}
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-bar-chart me-2"></i>统计信息</h5>
                </div>
                <div class="card-body">
                    <div class="row text-center">
                        <div class="col-12 mb-3">
                            <div class="border-bottom pb-2">
                                <h3 class="text-primary mb-0">{{ stats.total_assessments }}</h3>
                                <small class="text-muted">总测评次数</small>
                            </div>
                        </div>
                        <div class="col-12 mb-3">
                            <div class="border-bottom pb-2">
                                <h3 class="text-success mb-0">{{ stats.completed_assessments }}</h3>
                                <small class="text-muted">完成次数</small>
                            </div>
                        </div>
                        <div class="col-12">
                            <h3 class="text-info mb-0">{{ stats.completion_rate }}%</h3>
                            <small class="text-muted">完成率</small>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- 量表题目信息 -->
    {% if scale.questions %}
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="mb-0"><i class="bi bi-list-ol me-2"></i>题目信息</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th width="10%">题号</th>
                            <th width="50%">题目内容</th>
                            <th width="15%">题目类型</th>
                            <th width="25%">选项数量</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for question in scale.questions[:10] %}
                        <tr>
                            <td>{{ question.question_number }}</td>
                            <td>{{ question.question_text[:100] }}{% if question.question_text|length > 100 %}...{% endif %}</td>
                            <td>
                                {% if question.question_type == 'single_choice' %}
                                    <span class="badge bg-primary">单选</span>
                                {% elif question.question_type == 'multiple_choice' %}
                                    <span class="badge bg-info">多选</span>
                                {% elif question.question_type == 'text' %}
                                    <span class="badge bg-secondary">文本</span>
                                {% elif question.question_type == 'scale' %}
                                    <span class="badge bg-warning">量表</span>
                                {% endif %}
                            </td>
                            <td>{{ question.options|length if question.options else 0 }} 个</td>
                        </tr>
                        {% endfor %}
                        {% if scale.questions|length > 10 %}
                        <tr>
                            <td colspan="4" class="text-center text-muted">
                                还有 {{ scale.questions|length - 10 }} 道题目...
                            </td>
                        </tr>
                        {% endif %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    {% endif %}
    
    <!-- 最近测评记录 -->
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0"><i class="bi bi-file-text me-2"></i>最近测评记录</h5>
            <a href="/admin-assessments?scale_id={{ scale.id }}" class="btn btn-outline-primary btn-sm">
                查看全部记录
            </a>
        </div>
        <div class="card-body">
            {% if recent_assessments %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>测评ID</th>
                                <th>用户</th>
                                <th>状态</th>
                                <th>进度</th>
                                <th>开始时间</th>
                                <th>完成时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for assessment in recent_assessments %}
                            <tr>
                                <td>{{ assessment.id }}</td>
                                <td>
                                    {% if assessment.user %}
                                        <a href="/admin/users/{{ assessment.user.id }}" class="text-decoration-none">
                                            {{ assessment.user.username }}
                                        </a>
                                    {% else %}
                                        <span class="text-muted">匿名用户</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if assessment.status.value == 'completed' %}
                                        <span class="badge bg-success">已完成</span>
                                    {% elif assessment.status.value == 'in_progress' %}
                                        <span class="badge bg-warning">进行中</span>
                                    {% elif assessment.status.value == 'abandoned' %}
                                        <span class="badge bg-secondary">已放弃</span>
                                    {% endif %}
                                </td>
                                <td>
                                    <div class="progress" style="width: 80px; height: 20px;">
                                        <div class="progress-bar" role="progressbar" 
                                             style="width: {{ (assessment.progress_percentage or 0) }}%"
                                             aria-valuenow="{{ (assessment.progress_percentage or 0) }}" 
                                             aria-valuemin="0" aria-valuemax="100">
                                            {{ (assessment.progress_percentage or 0)|round|int }}%
                                        </div>
                                    </div>
                                </td>
                                <td>{{ assessment.started_at.strftime('%Y-%m-%d %H:%M') if assessment.started_at else '-' }}</td>
                                <td>{{ assessment.completed_at.strftime('%Y-%m-%d %H:%M') if assessment.completed_at else '-' }}</td>
                                <td>
                                    <a href="/admin-assessments" class="btn btn-outline-primary btn-sm" 
                                       onclick="viewAssessmentDetail({{ assessment.id }})">
                                        查看详情
                                    </a>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% else %}
                <p class="text-muted text-center py-4">该量表暂无测评记录</p>
            {% endif %}
        </div>
    </div>
    
    <!-- 操作按钮 -->
    <div class="mt-4">
        <a href="/admin-scales" class="btn btn-secondary">
            <i class="bi bi-arrow-left me-1"></i>返回量表列表
        </a>
        <a href="/admin/scale-edit?id={{ scale.id }}" class="btn btn-primary ms-2">
            <i class="bi bi-pencil me-1"></i>编辑量表
        </a>
        <a href="/scales/{{ scale.id }}/preview" target="_blank" class="btn btn-info ms-2">
            <i class="bi bi-eye me-1"></i>预览量表
        </a>
        {% if scale.status == 'PUBLISHED' %}
            <button class="btn btn-warning ms-2" onclick="toggleScaleStatus({{ scale.id }}, false)">
                <i class="bi bi-archive me-1"></i>归档量表
            </button>
        {% elif scale.status == 'ARCHIVED' %}
            <button class="btn btn-success ms-2" onclick="toggleScaleStatus({{ scale.id }}, true)">
                <i class="bi bi-check-circle me-1"></i>发布量表
            </button>
        {% elif scale.status == 'DRAFT' %}
            <button class="btn btn-success ms-2" onclick="toggleScaleStatus({{ scale.id }}, true)">
                <i class="bi bi-upload me-1"></i>发布量表
            </button>
        {% endif %}
    </div>
</div>

<script>
function toggleScaleStatus(scaleId, enable) {
    const action = enable ? '发布' : '归档';
    if (confirm(`确定要${action}该量表吗？`)) {
        fetch(`/api/admin/scales/${scaleId}/toggle-status`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + localStorage.getItem('adminToken')
            },
            body: JSON.stringify({ is_active: enable })
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert(`${action}成功`);
                location.reload();
            } else {
                alert(`${action}失败: ` + data.message);
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert(`${action}失败`);
        });
    }
}
</script>
{% endblock %}